<?php

include_once(APP_URL.'protected/myUtil/CheckUtil.php');
class DeviceController extends Controller {

    public function actionPostGpm() {
        // http://112.74.203.16/wifishare_server/index.php?r=device/postgpm
    
        // check param
        $paramObj = json_decode(CheckUtil::checkPostParam(110201, 'gpmList', false));
        if (!is_object($paramObj) || empty($paramObj)) {
            echo EchoUtil::retFailed(110201, 'param invalid !');
            exit;
        }
    
        $deviceId = $paramObj->deviceId;
        $deviceUserId = $paramObj->deviceUserId;
        $gpmInfo = $paramObj->gpmInfo;
        $TblGpmObj = new TblGpm();
    
        foreach ($gpmInfo as $value) {
            $conditionArr = array(
                'condition' => '
                     gpm_did=:gpmDid AND
                     gpm_duid=:gpmDuid AND
                     gpm_wid=:gpmWid AND
                     gpm_wuid=:gpmWuid',
                'params' => array(
                    'gpmDid'=>$deviceId,
                    'gpmDuid'=>$deviceUserId,
                    'gpmWid'=>$value->wifiId,
                    'gpmWuid'=>$value->wifiUserId)
            );// fill condition
            $result = $TblGpmObj->selectRecord($conditionArr);// select record
            if (!$result) {// not exsit add record
                $recordArr = array(
                    'gpm_did'=>$deviceId,
                    'gpm_duid'=>$deviceUserId,
                    'gpm_wid'=>$value->wifiId,
                    'gpm_wuid'=>$value->wifiUserId,
                    'gpm_count'=>$value->wifiGpm
                );
                if (!$TblGpmObj->addRecord('gpm', $recordArr)) {// add record
                    echo EchoUtil::retFailed(110202, 'post failed !');
                    exit;
                }
            } else {
                $conditionArr = array(
                    'condition' => 'gpm_id=:gpmId',
                    'params' => array(
                        'gpmId'=>$result->gpm_id
                    ));// fill condition
                $contentArr = array(
                    'gpm_count'=>($value->wifiGpm + $result->gpm_count)// new count is the total of old and this time
                );// fill content
                if (!$TblGpmObj->updateRecord($conditionArr, $contentArr)) {// update record
                    echo EchoUtil::retFailed(110202, 'post failed !');
                    exit;
                }
            }
        }
    
        // return success
        echo EchoUtil::retSucc(1, (object)array());
        
    }
    
    public function actionPostBind() {
        // http://112.74.203.16/wifishare_server/index.php?r=device/postbind
    
        // check param
        $userId = CheckUtil::checkPostParam(110701, 'userId', false);
        $userSession = CheckUtil::checkPostParam(110701, 'session', false);
        $deviceMac = CheckUtil::checkPostParam(110701, 'deviceMac', false);
        
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(110701, 'user not exsit !');
            exit;
        }
    
        // device not exsit
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'device_mac=:deviceMac',
            'params' => array('deviceMac'=>$deviceMac)
        );// fill condition
        $TblDevice = new TblDevice();
        $result = $TblDevice->selectRecord($conditionArr);
        if (!$result) {// device not exsit
            echo EchoUtil::retFailed(110701, 'device not exsit !');
            exit;
        }

        // device has been bound
        if ($result->device_uid) {// device has been bound
            echo EchoUtil::retFailed(110703, 'device has been bound !');
            exit;
        }

        // bind device
        $conditionArr = array(
            'condition' => 'device_id=:deviceId',
            'params' => array('deviceId'=>$result->device_id)
        );// fill condition
        $contentArr = array(
            'device_uid'=>$userId
        );// fill content
        if (!$TblDevice->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(110702, 'bind failed !');
            exit;
        }

        // return success
        echo EchoUtil::retSucc(1, array(
            'deviceId'=>$result->device_id
        ));
    
    }
    
    
    public function actionPostUnbind() {
        // http://112.74.203.16/wifishare_server/index.php?r=device/postunbind
    
        // check param
        $userId = CheckUtil::checkPostParam(110801, 'userId', false);
        $userSession = CheckUtil::checkPostParam(110801, 'session', false);
        $deviceId = CheckUtil::checkPostParam(110801, 'deviceId', false);
        
        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(110801, 'user not exsit !');
            exit;
        }
    
        // device not exsit
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'device_id=:deviceId',
            'params' => array('deviceId'=>$deviceId)
        );// fill condition
        $TblDevice = new TblDevice();
        $result = $TblDevice->selectRecord($conditionArr);
        if (!$result) {// device not exsit
            echo EchoUtil::retFailed(110801, 'device not exsit !');
            exit;
        }

        // device not been bound
        if (!$result->device_uid) {// device has been bound
            echo EchoUtil::retFailed(110803, 'device not been bound !');
            exit;
        }
        
        // unbind device
        $conditionArr = array(
            'condition' => 'device_id=:deviceId',
            'params' => array('deviceId'=>$result->device_id)
        );// fill condition
        $contentArr = array(
            'device_uid'=>NULL
        );// fill content
        if (!$TblDevice->updateRecord($conditionArr, $contentArr)) {// update record
            echo EchoUtil::retFailed(110802, 'unbind failed !');
            exit;
        }

        // return success
        echo EchoUtil::retSucc(1, (object)array());
    
    }


    public function actionGetInfo() {
        // http://112.74.203.16/wifishare_server/index.php?r=device/getinfo
    
        // param check
        $userId = CheckUtil::checkGetParam(100301, 'userId', false);
        $userSession = CheckUtil::checkGetParam(100301, 'session', false); 

        // identify user
        $result = $this->userIdentify($userId, $userSession);
        if (!$result) {
            echo EchoUtil::retFailed(100301, 'user not exsit !');
            exit;
        } 
  
        // user has not bound any device
        $conditionArr = array(
            'select' => array('*'),
            'condition' => 'device_uid=:deviceUid',
            'params' => array('deviceUid'=>$userId)
        );// fill condition
        $TblDevice = new TblDevice();
        $result = $TblDevice->selectRecord($conditionArr);
        if (!$result) {// user has not bound any device
            echo EchoUtil::retFailed(100302, 'user has not bound any device !');
            exit;
        }
    
        // return success
        echo EchoUtil::retSucc(1, array(
            'deviceId' => $result->device_id,
            'deviceMac' => $result->device_mac
        ));
    
    }
    
    public function actionGetCrt() {
        // http://112.74.203.16/wifishare_server/index.php?r=device/getcrt
        
        // check param
        $deviceId = CheckUtil::checkGetParam(100101, 'deviceId', false);
        
        $conditionArr = array(
            'select' => array('device_crturl'),
            'condition' => 'device_id=:deviceId',
            'params' => array('deviceId'=>$deviceId));// fill condition

        $TblDeviceObj = new TblDevice();
        $result = $TblDeviceObj->selectRecord($conditionArr);
        if (!$result) {// not exsit vpncrt 
            echo 'generate crt ... ';
            // generate crt
            // ...
            
            echo EchoUtil::retFailed(100102, 'get vpncrt failed !');
            exit;
        }
        
        $vpncrtUrl = is_null($result->device_crturl) ? "" : $result->device_crturl;
        
        // return success
        echo EchoUtil::retSucc(1, array('vpncrtUrl'=>$vpncrtUrl));
        
    }
    
}
